﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using bdnt_pets_system.DataServiceReference;
using bdnt_pets_system_Entity;

namespace bdnt_pets_system.PageViews.DeptManager
{
    public partial class DeptManagerControl : UserControl
    {
        DataServiceReference.DataServiceClient dataClient;

        public DeptManagerControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            InitEvnet();
            QueryData();

        }
        void QueryData()
        {
            dataClient.SelectDepartmentAsync(tbMc.Text); 
            
        }

        void InitEvnet()
        {
            dataClient.SelectDepartmentCompleted += new EventHandler<DataServiceReference.SelectDepartmentCompletedEventArgs>(dataClient_SelectDepartmentCompleted);
            dataClient.DeleteDepartmentCompleted += new EventHandler<DataServiceReference.DeleteDepartmentCompletedEventArgs>(dataClient_DeleteDepartmentCompleted);

            btnSs.Click += new RoutedEventHandler(btnSs_Click);
            btnCs.Click += new RoutedEventHandler(btnCs_Click);
            btnNewObj.Click += new RoutedEventHandler(btnNewObj_Click);
        }
        
        void btnNewObj_Click(object sender, RoutedEventArgs e)
        {
            var newdept = new NewDeptControl();
            App.ToPage(newdept);
        }

     
        void btnCs_Click(object sender, RoutedEventArgs e)
        {
            tbMc.Text = "";
        }

  
        void btnSs_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }
    
        void dataClient_DeleteDepartmentCompleted(object sender, DataServiceReference.DeleteDepartmentCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("删除成功！", "系统提示", MessageBoxButton.OK);
                QueryData();
            }
            else
            {
                MessageBox.Show("删除失败！", "系统提示", MessageBoxButton.OK);
                return;
            }
        }

        void dataClient_SelectDepartmentCompleted(object sender, DataServiceReference.SelectDepartmentCompletedEventArgs e)
        {
            dgList.ItemsSource = e.Result;
        }

        private void hbDelete_Click(object sender, RoutedEventArgs e)
        {
            string dId = (sender as HyperlinkButton).Tag.ToString();
            if (dId == null)
            {
                return;
            }
            MessageBoxResult result = MessageBox.Show("确定要删除吗？", "系统提示", MessageBoxButton.OKCancel);
            if (MessageBoxResult.OK == result)
            {
                dataClient.DeleteDepartmentAsync(Int32.Parse(dId));
            }
        }

        private void hbUpdate_Click(object sender, RoutedEventArgs e)
        {
            var dId = (sender as HyperlinkButton).Tag as Tbl_Department;
            var newdept = new NewDeptControl(dId);
            App.ToPage(newdept);
        }

    }
}
